# import os
import arcpy

str=arcpy.GetParameterAsText(0)
gdblist = str.split(";")
print(gdblist)
arcpy.AddMessage(gdblist)

outpath="D:\\Desktop"
outgdbname="合并数据库"
gdbpath= outpath + "\\" + outgdbname + ".gdb"
if arcpy.Exists(gdbpath):
    pass
else:
    arcpy.CreateFileGDB_management(outpath,outgdbname)

for gdb in gdblist:
    print(gdb)
    arcpy.AddMessage(gdb)
    arcpy.env.workspace = gdb
    datasets = arcpy.ListDatasets()
    for ds in datasets:
        print("  " + ds)
        arcpy.AddMessage("  " + ds)
        if arcpy.Exists(gdbpath + "\\" + ds):
            pass
        else:
            desc = arcpy.Describe(ds)
            sr = desc.spatialReference
            arcpy.CreateFeatureDataset_management(gdbpath, ds, sr)

        fcs=arcpy.ListFeatureClasses(feature_dataset=ds)
        for fc in fcs:
            print("    " + fc)
            arcpy.AddMessage("    " + fc)
            if arcpy.Exists(gdbpath + "\\" + ds + "\\" + fc):
                arcpy.Append_management(gdb + "\\" + ds + "\\" + fc,
                                        gdbpath + "\\" + ds + "\\" + fc,
                                        "NO_TEST")
            else:
                arcpy.FeatureClassToFeatureClass_conversion(fc, 
                                                            gdbpath + "\\" + ds, 
                                                            fc)
                
